package
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.text.TextField;
	import states.GameStateManager;
	import utilities.KeyboardInputManager;
	import utilities.Profiler;
	
	/**
	 * ...
	 * @author racebuilders
	 */
	public class Main extends Sprite
	{
		/* private const/var */
		private static var m_txtDebug:TextField = null;
		private var m_profiler:Profiler = null;
		private var m_gameStageMgr:GameStateManager = null;
		
		/* public functions */
		public function Main():void
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		public static function get txtDebug():TextField
		{
			return m_txtDebug;
		}
		
		public static function set txtDebug(debug:TextField):void
		{
			m_txtDebug = debug;
		}
		
		/* private functions */
		private function init(e:Event = null):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			// entry point
			
			CONFIG::debug
			{
				m_txtDebug = new TextField();
				m_txtDebug.width = 150;
				m_txtDebug.height = stage.stageHeight;
				m_txtDebug.x = stage.stageWidth - m_txtDebug.width;
				m_txtDebug.y = 0;
				m_txtDebug.border = true;
				m_txtDebug.background = true;
				m_txtDebug.text = "Debug";
				
				if (!stage.contains(m_txtDebug))
				{
					stage.addChild(m_txtDebug);
				}
			}
			
			m_profiler = new Profiler(640, 0, stage);
			
			m_gameStageMgr = new GameStateManager();
			m_gameStageMgr.initialize(stage);
			//addEventListener(Event.ENTER_FRAME, Update, false, 0 , true);
			if (!stage.hasEventListener(Event.ENTER_FRAME))
			{
				stage.addEventListener(Event.ENTER_FRAME, update, false, 0, true);
			}
		}
		
		private function update(e:Event):void
		{
			/*CONFIG::debug
			{
				if (m_txtDebug && stage.contains(m_txtDebug))
				{
					stage.removeChild(m_txtDebug);
				}
			}
			if (m_profiler && stage.contains(m_profiler.textfield))
			{
				stage.removeChild(m_profiler.textfield);
			}*/
			
			m_profiler.update();
			m_gameStageMgr.update();
			
			/*if (m_profiler && !stage.contains(m_profiler.textfield))
			{
				stage.addChild(m_profiler.textfield);
			}
			CONFIG::debug
			{
				if (m_txtDebug && !stage.contains(m_txtDebug))
				{
					stage.addChild(m_txtDebug);
				}
			}*/
		}
		
	}
	
}
